<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">考试信息</el-breadcrumb-item>
      <el-breadcrumb-item>考试信息列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="创建时间">
              <span>{{ props.row.createTime }}</span>
            </el-form-item>
            <el-form-item label="更新时间">
              <span>{{ props.row.updateTime }}</span>
            </el-form-item>
            <el-form-item label="创建人">
              <span>{{ props.row.userId }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="title" label="标题" width="200" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="description" label="描述" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="rule" label="规则" width="100" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="examContent" label="考试信息内容" width="100" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="grade" label="等级" width="100" align="center"></el-table-column>
      <el-table-column prop="certificate" label="证书" width="100" align="center"></el-table-column>
      <el-table-column prop="examType" label="考试类型" width="100" align="center"></el-table-column>

      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="showEditFormDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     :disabled="scope.row.isParent == 1"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 以下是修改考试信息的弹出表单Dialog -->
    <el-dialog title="修改类别" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
          <el-form-item label="标题" prop="parentId">
            <el-input v-model="editForm.title" placeholder="请输入考试信息标题"></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="name">
            <el-input v-model="editForm.description"
                      placeholder="请输入描述内容"></el-input>
          </el-form-item>
          <el-form-item label="规则" prop="keywords">
            <el-input v-model="editForm.rule"
                      placeholder="请输入考试规则内容"></el-input>
          </el-form-item>
          <el-form-item label="信息内容" prop="icon">
            <el-input v-model="editForm.examContent"
                      type="textarea"
                      :autosize="{ minRows: 5, maxRows: 8}"
                      placeholder="请输入信息内容"></el-input>
          </el-form-item>
          <el-form-item label="考试等级" prop="sort">
            <el-input v-model="editForm.grade"></el-input>
          </el-form-item>
          <el-form-item label="考试证书" prop="sort">
            <el-input v-model="editForm.certificate"></el-input>
          </el-form-item>
<!--        <el-form-item label="名称" :label-width="formLabelWidth">-->
<!--          <el-input v-model="editForm.name" autocomplete="off"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="关键词列表" :label-width="formLabelWidth">-->
<!--          <el-input v-model="editForm.keywords" autocomplete="off"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="排序序号" :label-width="formLabelWidth">-->
<!--          <el-input v-model="editForm.sort" autocomplete="off"></el-input>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 100px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  padding-left: 110px;
  margin-right: 10px;
  margin-bottom: 0;
  width: 50%;
}
</style>
<script>
export default {
  data() {
    return {
      tableData: [],
      currentParentId: 0,
      history: [],
      dialogFormVisible: false,
      editForm: {
        title:'',
        description:'',
        rule:'',
        examContent:'',
        grade:'',
        certificate:''
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    showEditFormDialog(examIntroduction) {
      this.editForm = examIntroduction;
      this.dialogFormVisible = true;
      console.log(examIntroduction)
      let url = 'http://120.77.154.166:8080/examIntroduction/' + examIntroduction.id+'select';;
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadExamIntroductionList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },

    submitEditForm() {
      let url = 'http://120.77.154.166:8080/examIntroduction/' + this.editForm.id + '/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.editForm);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改考试信息成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadExamIntroductionList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadExamIntroductionList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$message.error(jsonResult.message);
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
            }
          });
        }
      });
    },
    openDeleteConfirm(examIntroduction) {
      let message = '此操作将永久删除【' + examIntroduction.title + '】考试信息，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(examIntroduction);
      }).catch(() => {
      });
    },
    handleDelete(examIntroduction) {
      let url = 'http://120.77.154.166:8080/examIntroduction/' + examIntroduction.id + '/delete';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除【' +examIntroduction.title + '】考试信息成功！',
            type: 'success'
          });
          this. loadExamIntroductionList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadExamIntroductionList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadExamIntroductionList();
            }
          });
        }
      });
    },
    loadExamIntroductionList() {
      let url = 'http://120.77.154.166:8080/examIntroduction/list';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data;
        }
      });
    }
  },
  mounted() {
    this.loadExamIntroductionList();
  }
}
</script>
